package com.tfswx.jcyw.sqlmodel.api.function;

import com.googlecode.aviator.AviatorEvaluator;
import com.googlecode.aviator.Expression;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 张述江
 * @date 2024/03/09
 */
public class FunctionUtil {

    public static final Map<String, RuntimeFuntion> FUNCTIONS = new HashMap<>();

    public static void addFunction(RuntimeFuntion function) {
        AviatorEvaluator.addFunction(function);
        FUNCTIONS.put(function.getName(), function);
    }

    public static void addFunctions(List<RuntimeFuntion> functionList) {
        if (functionList != null) {
            for (RuntimeFuntion runtimeFuntion : functionList) {
                addFunction(runtimeFuntion);
            }
        }
    }

    public static Object execute(String exp, Map<String, Object> env) {
        Expression compiledExp = AviatorEvaluator.compile(exp, true);
        return compiledExp.execute(env);
    }
}
